package com.awesome.spring.drools.controller;

import com.awesome.spring.drools.config.DroolsConfig;
import com.awesome.spring.drools.model.Order;
import org.kie.api.runtime.KieContainer;
import org.kie.api.runtime.KieSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @program: awesome-spring
 * @description: drools控制器类
 * @author: Alex Wu
 * @createDate: 2025-03-26 16:10
 **/

@RestController
@RequestMapping("/drools")
public class DroolsController {

    @Autowired
    private KieContainer kieContainer;


    @GetMapping("/test/{amount}")
    public String test(@PathVariable("amount") Double amount) {
        KieSession session = kieContainer.newKieSession();

        //Fact对象，事实对象
        Order order = new Order();
        order.setAmout(amount);

        //将Order对象插入到工作内存中
        session.insert(order);

        //激活规则，由Drools框架自动进行规则匹配，如果规则匹配成功，则执行当前规则
        session.fireAllRules();
        //关闭会话
        session.dispose();

        System.out.println("订单金额：" + order.getAmount() +
                "，添加积分：" + order.getScore());
        return "OK";
    }
}
